from django.db import models


# Create your models here.
class UserInfo(models.Model):
    """用户信息表"""
    
    # 给name创建索引
    name = models.CharField(verbose_name='用户名', max_length=32, db_index=True)
    email = models.EmailField(verbose_name='邮箱', max_length=32)
    phone = models.CharField(verbose_name='手机', max_length=32)
    password = models.CharField(verbose_name='密码', max_length=32)
    
    # 可以提升性能
    # price_policy = models.ForeignKey(verbose_name='价格策略', to='PricePolicy', null=True, blank=True)
    
    def __str__(self):
        return self.name


class PricePolicy(models.Model):
    """价格策略表"""
    
    category_choices = (
        (1, '个人免费版'),
        (2, '收费版'),
        (3, '其他'),
    )
    
    category = models.SmallIntegerField(verbose_name='收费类型', default=2, choices=category_choices)
    title = models.CharField(max_length=24, verbose_name='标题', blank=True, null=True)
    # 正整数字段
    price = models.PositiveIntegerField(verbose_name='价格', default=0)
    project_num = models.PositiveIntegerField(verbose_name='项目数量', default=0)
    project_member = models.PositiveIntegerField(verbose_name='项目成员数量', default=0)
    project_space = models.PositiveIntegerField(verbose_name='项目空间', default=0, help_text='G')
    project_file_size = models.PositiveIntegerField(verbose_name='文件大小', default=0, help_text='M')
    
    c_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)


class Transaction(models.Model):
    """交易记录表"""
    
    status_choices = (
        (1, '未支付'),
        (2, '已支付')
    )
    
    status = models.SmallIntegerField(verbose_name='状态', default=0, choices=status_choices)
    order_num = models.CharField(max_length=64, verbose_name='订单号', unique=True)
    count = models.IntegerField(verbose_name='数量/年', help_text='0为无限期', blank=True, null=True)
    price = models.IntegerField(verbose_name='实付', blank=True, null=True)
    start_time = models.DateTimeField(verbose_name='开始时间', blank=True, null=True)
    end_time = models.DateTimeField(verbose_name='结束时间', blank=True, null=True)
    c_time = models.DateTimeField(verbose_name='创建时间', blank=True, null=True)
    
    user = models.ForeignKey(to=UserInfo, verbose_name='用户', on_delete=models.CASCADE)
    price_policy = models.ForeignKey(to=PricePolicy, verbose_name='价格策略', on_delete=models.CASCADE)


class Project(models.Model):
    """项目表"""
    color_choices = (
        (1, '#56b8eb'),
        (2, '#f28033'),
        (3, '#ebc656'),
        (4, '#a2d148'),
        (5, '#20bfa4'),
        (6, '#7461c2'),
        (7, '#20bfa3'),
    )
    project_name = models.CharField(max_length=24, verbose_name='项目名')
    color = models.SmallIntegerField(verbose_name='颜色', choices=color_choices, default=1)
    desc = models.CharField(max_length=256, verbose_name='项目描述', blank=True, null=True)
    use_space = models.BigIntegerField(verbose_name='项目已使用空间', default=0, help_text='B')
    star = models.BooleanField(verbose_name='星标', default=False)
    bucket = models.CharField(max_length=128, verbose_name='腾讯对象存储桶', blank=True, null=True)
    region = models.CharField(max_length=32, verbose_name='腾讯对象存储桶区域', blank=True, null=True)
    join_count = models.IntegerField(verbose_name='参与人数', default=1)
    creator = models.ForeignKey(to=UserInfo, verbose_name='创建者', on_delete=models.CASCADE)
    c_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    
    # p_u = models.ManyToManyField(to='Something', through='tmp', through_fields=('one', 'two'))


class Participant(models.Model):
    """
    项目参与者
    一个表的字段有两个跟另一个表有关联时，得使用反向关联，避免冲突
    """
    
    star = models.SmallIntegerField(verbose_name='星标', default=False)
    c_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    user = models.ForeignKey(to=UserInfo, verbose_name='用户', on_delete=models.CASCADE)
    project = models.ForeignKey(to=Project, verbose_name='项目', on_delete=models.CASCADE)
    invites = models.ForeignKey(to=UserInfo, verbose_name='邀请者', related_name='invites', blank=True, null=True,
                                on_delete=models.CASCADE)


class Wiki(models.Model):
    """
    wiki
    """
    
    project = models.ForeignKey(to=Project, verbose_name='项目', on_delete=models.CASCADE)
    title = models.CharField(max_length=24, verbose_name='标题')
    content = models.TextField(verbose_name='内容')
    # 解决某些文章无法显示
    depth = models.IntegerField(verbose_name='深度', default=1)
    
    # 自关联  related_name='children':反向关联的名字
    parent = models.ForeignKey(to='self', verbose_name='父文章', null=True, blank=True, on_delete=models.CASCADE,
                               related_name='children')
    
    def __str__(self):
        return self.title


class FileRepository(models.Model):
    """文件"""
    
    file_type_choices = (
        (1, '文件'),
        (2, '文件夹')
    )
    
    file_type = models.SmallIntegerField(verbose_name='类型', choices=file_type_choices)
    name = models.CharField(verbose_name='文件名称', max_length=24, help_text='文件/文件夹名称')
    key = models.CharField(verbose_name='文件存储真cos的key', max_length=128, null=True, blank=True)
    file_size = models.BigIntegerField(verbose_name='文件大小', null=True, blank=True, help_text='B')
    # 文件下载路径
    file_path = models.CharField(verbose_name='文件路径', max_length=255, null=True, blank=True)
    parent = models.ForeignKey(verbose_name='父级目录', to='self', related_name='child', null=True, blank=True,
                               on_delete=models.CASCADE)
    update_user = models.ForeignKey(verbose_name='更新者', to='UserInfo', on_delete=models.CASCADE)
    update_time = models.DateTimeField(verbose_name='更新时间', auto_now=True)
    project = models.ForeignKey(verbose_name='项目', to='Project', on_delete=models.CASCADE)
    
    def __str__(self):
        return self.name


class Issues(models.Model):
    """问题"""
    
    priority_choices = (('danger', '高'), ('warning', '中'), ('success', '低'))
    status_choices = ((1, '新建'), (2, '处理中'), (3, '已解决'), (4, '已忽略'), (5, '待反馈'), (6, '已关闭'), (7, '重新打开'))
    mode_choices = ((1, '公开模式'), (2, '私有模式'))
    
    subject = models.CharField(max_length=60, verbose_name='主题')
    desc = models.TextField(verbose_name='问题描述')
    priority = models.CharField(max_length=24, verbose_name='优先级', choices=priority_choices, default='danger')
    status = models.SmallIntegerField(verbose_name='状态', choices=status_choices, default=1)
    mode = models.SmallIntegerField(verbose_name='模式', choices=mode_choices, default=1)
    
    assign = models.ForeignKey(to='UserInfo', verbose_name='指派', related_name='task', null=True, blank=True,
                               on_delete=models.CASCADE)
    attention = models.ManyToManyField(to='UserInfo', verbose_name='关注者', related_name='observe', blank=True)
    parent = models.ForeignKey(to='self', verbose_name='父问题', related_name='child', on_delete=models.CASCADE, null=True,
                               blank=True)
    creator = models.ForeignKey(to='UserInfo', verbose_name='创建者', related_name='create_problems',
                                on_delete=models.CASCADE)
    project = models.ForeignKey(to='Project', verbose_name='项目', on_delete=models.CASCADE)
    issues_type = models.ForeignKey(to='IssuesType', verbose_name='问题类型', on_delete=models.CASCADE)
    module = models.ForeignKey(to='Module', verbose_name='模块', null=True, blank=True, on_delete=models.CASCADE)
    
    create_datetime = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    latest_update_datetime = models.DateTimeField(verbose_name='最近更新时间', auto_now=True)
    start_date = models.DateTimeField(verbose_name='开始时间', null=True, blank=True)
    end_date = models.DateTimeField(verbose_name='结束时间', null=True, blank=True)
    
    def __str__(self):
        return self.subject


class Module(models.Model):
    """模块 （进度）"""
    
    title = models.CharField(max_length=24, verbose_name='模块名称')
    project = models.ForeignKey(to='Project', verbose_name='项目', on_delete=models.CASCADE)
    
    def __str__(self):
        return self.title


class IssuesType(models.Model):
    """问题类型"""
    
    # 默认创建
    PROJECT_INIT_LIST = ['任务', '功能', 'BUG']
    
    title = models.CharField(max_length=24, verbose_name='类型名称')
    project = models.ForeignKey(to='Project', verbose_name='项目', on_delete=models.CASCADE)
    
    def __str__(self):
        return self.title


class IssuesReply(models.Model):
    """问题回复"""
    
    reply_choices = ((1, '修改记录'), (2, '回复'))
    
    reply_type = models.SmallIntegerField(verbose_name='类型', choices=reply_choices)
    content = models.TextField(verbose_name='描述')
    create_datetime = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    
    creator = models.ForeignKey(to='UserInfo', verbose_name='创建者', related_name='create_reply', on_delete=models.CASCADE)
    issues = models.ForeignKey(to='Issues', verbose_name='问题', on_delete=models.CASCADE)
    reply = models.ForeignKey(to='self', verbose_name='回复', null=True, blank=True, on_delete=models.CASCADE)


class ProjectInvite(models.Model):
    """项目邀请码"""

    period_choices = (
        (30, '30分钟'),
        (60, '1小时'),
        (300, '30小时'),
        (1440, '24小时'),
    )

    code = models.CharField(verbose_name='邀请码', max_length=64, unique=True)
    count = models.PositiveIntegerField(verbose_name='限制数量', null=True, blank=True, help_text='空表示不限量')
    use_count = models.PositiveIntegerField(verbose_name='已邀请', default=0)
    period = models.IntegerField(verbose_name='有效期', choices=period_choices, default=1440)

    project = models.ForeignKey(to='Project', verbose_name='项目', on_delete=models.CASCADE)
    create_datetime = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    creator = models.ForeignKey(to='UserInfo', verbose_name='创建者', related_name='create_invite', on_delete=models.CASCADE)
